var gpsToAddress = async function (gps) {
  var reg = /^\d{4}.\d{4},N,\d{5}.\d{4},E$/;
  var trr = {};
  if (gps.match(reg)) {
    let arr = gps.split(",");
    trr.x = arr[2];
    trr.y = arr[0];
    trr.x = Point(trr.x);
    trr.y = Point(trr.y);
  };
  var xxx = 123;
  var cf = await convertFrom(trr);
  var keyboard = await mapAddress(cf);
  // console.log(keyboard)
  return keyboard;

  function convertFrom(trr){
    var lnglat1 = new AMap.LngLat(trr.x, trr.y);
    // console.log('----before:' +lnglat1)
    return new Promise((resolve, reject) => {
      new AMap.convertFrom(lnglat1, "gps", function (status, result) {
        if (status == "complete") {
          var address = {};
          address.x = result.locations[0].lng + 0.00012;
          address.y = result.locations[0].lat + 0.00002;
          resolve(address);
        } else {
          alert("请再次刷新页面!");
        }
      });
    });
  }
  //-------获取地图详细地址--------
  function mapAddress(address) {
    return new Promise((resolve, reject) => {
      var geocoder = new AMap.Geocoder({
        city: "010" //城市，默认：“全国”
      });
      var lnglatXY = [address.x, address.y]; //地图上所标点的坐标
      geocoder.getAddress(
        lnglatXY,
        function (status, result) {
          if (status === "complete" && result.info === "OK") {
            resolve(result.regeocode.formattedAddress);
          } else {
            console.log("获取地址失败");
          }
        }.bind(this)
      );
    });
  }
  //----------度分转换------------
  function Point(dm) {
    var mins = dm - parseInt(dm);
    var value =
      parseInt(parseInt(dm) / 100) +
      ((parseInt(dm) / 100 - parseInt(parseInt(dm) / 100)) * 100 + mins) / 60;
    return value;
  }
}

export default gpsToAddress;
